package com.systemsjr.mpf.expense;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

import com.systemsjr.mpf.IntentHandler;
import com.systemsjr.mpf.MpfActivity;
import com.systemsjr.mpf.R;
import com.systemsjr.mpf.account.Account;
import com.systemsjr.mpf.account.AccountInstance;
import com.systemsjr.mpf.account.ManageAccountActivity;

public class ExpenseList extends ListActivity {

	private SimpleCursorAdapter adapter;
	private ListView listView;
	private Cursor c;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_expense_list);

		String[] from = { Expense.DESCRIPTION, Expense.AMOUNT,
				Expense.EXPENSE_DATE };

		c = getContentResolver().query(Expense.CONTENT_URI,
				Expense.PROJECTIONS, null, null, null);
		// String[] selectionArgs = { "%", "%", "%" };

		Expense exp = new Expense();

		Bundle bundle = getIntent().getExtras();

		if (bundle != null && bundle.size() > 0) {
			if (bundle.get(Expense.ACCOUNT_INSTANCE) != null) {
				exp.setAccountInstance((AccountInstance) bundle
						.get(Expense.ACCOUNT_INSTANCE));
			}

			if (bundle.get(Expense.EXPENSE_TYPE) != null) {
				exp.setExpenseType((ExpenseType) bundle
						.get(Expense.EXPENSE_TYPE));
			}

			if (bundle.getString(Expense.DESCRIPTION) != null) {
				exp.setDescription(bundle.getString(Expense.DESCRIPTION));
			}

			/*
			 * selectionArgs = new String[] { (exp.getDescription() != null &&
			 * exp.getDescription() .length() > 0) ? "%" : "%" +
			 * exp.getDescription(), exp.getAccountInstance() == null ? "0" : ""
			 * + exp.getAccountInstance().getId(), exp.getExpenseType() == null
			 * ? "0" : "" + exp.getExpenseType().getId() };
			 */
		}

		String selection = "DESCRIPTION LIKE "
				+ ((exp.getDescription() == null || exp.getDescription()
						.length() > 0) ? "'%'" : "'%" + exp.getDescription()
						+ "%'");

		selection = selection
				+ " AND "
				+ Expense.EXPENSE_TYPE
				+ (exp.getExpenseType() == null ? " > 0" : " = "
						+ exp.getExpenseType().getId());

		selection = selection
				+ " AND "
				+ Expense.ACCOUNT_INSTANCE
				+ (exp.getAccountInstance() == null ? " > 0 " : " =  "
						+ exp.getAccountInstance().getId());

		System.out.println(selection);

		c = getContentResolver().query(Expense.CONTENT_URI,
				Expense.PROJECTIONS, selection, null, null);
		startManagingCursor(c);

		int[] to = { R.id.exp_description, R.id.exp_amount,
				R.id.exp_expense_date };

		adapter = new SimpleCursorAdapter(this, R.layout.expense_list_entry, c,
				from, to, 0);
		listView = getListView();

		listView.setAdapter(adapter);

		Button expenseButton = (Button) findViewById(R.id.addExpenseButton);
		expenseButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(ExpenseList.this,
						ManageExpenseActivity.class);
				startActivity(intent);

			}
		});

		Button expenseTypeButton = (Button) findViewById(R.id.addExpenseTypeButton);
		expenseTypeButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				Intent intent = new Intent(ExpenseList.this,
						ManageExpenseTypeActivity.class);
				startActivity(intent);
			}
		});
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Intent intent = new Intent(ExpenseList.this,
				ManageExpenseActivity.class);

		if (adapter != null) {
			Cursor c = (Cursor) adapter.getItem(position);
			intent.putExtra(Expense.DESCRIPTION,
					c.getString(c.getColumnIndex(Expense.DESCRIPTION)));
			intent.putExtra(Expense.ID, c.getLong(c.getColumnIndex(Expense.ID)));
			intent.putExtra(Expense.ACCOUNT_INSTANCE,
					c.getLong(c.getColumnIndex(Expense.ACCOUNT_INSTANCE)));
			intent.putExtra(Expense.EXPENSE_TYPE,
					c.getLong(c.getColumnIndex(Expense.EXPENSE_TYPE)));
			intent.putExtra(Expense.EXPENSE_DATE,
					c.getString(c.getColumnIndex(Expense.EXPENSE_DATE)));
			System.out.println("The type is "
					+ c.getLong(c.getColumnIndex(Expense.EXPENSE_TYPE)));
			System.out.println("The date at list is "
					+ c.getString(c.getColumnIndex(Expense.EXPENSE_DATE)));
			// intent.putex
		}
		startActivityForResult(intent, 1);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.expense_list, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		Intent intent = IntentHandler.handleOnOptionsItemSelected(this,
				item.getItemId());
		startActivityForResult(intent, 1);
		return super.onOptionsItemSelected(item);
	}
}
